<?php
/**
 * 微博通知
 */
class Notice_MBlog 
{
	private $_passport, $_message, $_picurl;
	private $_url_message, $_app_key;
	private $_nickname;
	private $curl;

	public function __construct ()
	{
		$this->_url_message = Kohana::config('notice.microblog.url.message');
		$this->_url_activate= Kohana::config('notice.microblog.url.activate');
		$this->_app_key     = Kohana::config('notice.microblog.appkey');
		$this->curl = curl_init();
	}

	public function to ($uid)
	{
		$uid   = (int) $uid;
		$user  = user::factory($uid);
		$uinfo = $user->get_user_info();
		$passport = empty($uinfo) ? NULL : $uinfo['passport'];
		$this->_passport = $passport;
		$this->_nickname = $uinfo['nickname'];
		return $this;
	}
	
	public function message ($message)
	{
		$this->_message = trim($message);
		return $this;
	}

	public function picture ($url)
	{
		$this->_picurl = $url;
		return $this;
	}

	public function send ($message = NULL)
	{
		if (! empty($message))
			$this->_message = $message;

		$data = array(
			'appkey'      => $this->_app_key,
			'user_ip'     => Request::$client_ip,
			'passport'    => $this->_passport,
			'status'      => $this->_message,
			'trim_user'   => FALSE,
		);

		if (! empty($this->_picurl))
			$data['pic'] = $this->_picurl;

		$enable = Kohana::config('mblog.enable_send');
		if ($enable === TRUE)
			$this->json_post($this->_url_message, $data);
	}

	public function activate ()
	{
		$data = array(
			'appkey'   => $this->_app_key,
			'user_ip'  => Request::$client_ip,
			'passport' => $this->_passport,
			'screen_name' => $this->_nickname,
		);

		$this->json_post($this->_url_activate, $data);
	}

	private function json_post ($url, $arr_post_data)
	{
		$posts = array();

		foreach ($arr_post_data as $k => $v)
		{
			$posts[] = $k.'='.rawurlencode($v);
		}

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL , $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $posts));
		$r  = curl_exec($ch);

		curl_close($ch);
		return $r;
	}

	public function upload($status, $file)
 	{
  		$boundary = uniqid('------------------');
  		$MPboundary = '--'.$boundary;
  		$endMPboundary = $MPboundary. '--';

  		$multipartbody = '';
  		$multipartbody .= $MPboundary . "\r\n";
  		$multipartbody .= 'Content-Disposition: form-data; name="pic"; filename="wiki.jpg"'. "\r\n";
  		$multipartbody .= 'Content-Type: image/jpg'. "\r\n\r\n";
  		$multipartbody .= $file. "\r\n";

  		$k = "appkey";
  		$v = $this->_app_key;
  		$multipartbody .= $MPboundary . "\r\n";
  		$multipartbody.='content-disposition: form-data; name="'.$k."\"\r\n\r\n";
  		$multipartbody.=$v."\r\n";

  		$k = "user_ip";
  		$v = '219.232.228.228';
  		$multipartbody .= $MPboundary . "\r\n";
  		$multipartbody.='content-disposition: form-data; name="'.$k."\"\r\n\r\n";
  		$multipartbody.=$v."\r\n";


  		$k = "passport";
  		$v = 'xiaoliangyang@sohu.com';
  		$multipartbody .= $MPboundary . "\r\n";
  		$multipartbody.='content-disposition: form-data; name="'.$k."\"\r\n\r\n";
  		$multipartbody.=rawurlencode($v)."\r\n";



  		$k = "status";
  		$v = $status;
  		$multipartbody .= $MPboundary . "\r\n";
  		$multipartbody.='content-disposition: form-data; name="'.$k."\"\r\n\r\n";
  		$multipartbody.=rawurlencode($v)."\r\n";
  		$multipartbody .= "\r\n". $endMPboundary;

  		curl_setopt( $this->curl,  CURLOPT_RETURNTRANSFER, 1);
  		curl_setopt( $this->curl, CURLOPT_POST, 1 );
  		curl_setopt( $this->curl, CURLOPT_POSTFIELDS , $multipartbody );


  		$header_array = array("Content-Type: multipart/form-data; boundary=$boundary" , "Expect: ");

  		curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header_array);
  		curl_setopt($this->curl, CURLOPT_URL, $this->_url_message );
  		curl_setopt($this->curl, CURLOPT_HEADER , false );
  		curl_setopt($this->curl, CURLINFO_HEADER_OUT , true );

  		$info = curl_exec($this->curl);

  		return json_decode( $info , true);
 }

}
